package com.ejd.utils;

import java.util.List;

import javax.faces.application.FacesMessage;

import net.sf.cglib.beans.BeanCopier;

import com.ejd.service.IColorService;
import com.ejd.service.IGoodsService;
import com.ejd.service.ISizeService;
import com.ejd.web.bo.impl.Color;
import com.ejd.web.bo.impl.Goods;
import com.ejd.web.bo.impl.Size;
import com.ejd.web.constant.EjdConstants;
import com.ejd.web.constant.SpringBeanConstants;
import com.ejd.web.vo.shopcart.InventoryItem;

public final class InventoryUtil {
	public InventoryUtil() {
	  }
	public static InventoryItem getInventoryItemByParam(String code, String primaryCategoryCode,String specialField1, String specialField2){ 
        InventoryItem result = new InventoryItem();
        if (null == code || "".equals(code)) {
        	return result;
        }
        
    	IGoodsService<Goods,Long> goodsService = (IGoodsService<Goods,Long>)SpringFacesUtil.getSpringBean(SpringBeanConstants.GOODS_SERVICE_BEAN_NAME);
    	goodsService.setEntityClass(Goods.class);
    	goodsService.setPKClass(Long.class);
		Goods goods = goodsService.findUniqueBy("code", code);
		BeanCopier copyHere = BeanCopier.create(Goods.class,InventoryItem.class,false);
		copyHere.copy(goods, result, null);
		
		if (null != specialField1 && !("".equals(specialField1))) {
    		result.setSpecialField1(specialField1);
    		IColorService<Color,Long> colorService = (IColorService<Color,Long>)SpringFacesUtil.getSpringBean(SpringBeanConstants.COLOR_SERVICE_BEAN_NAME);
    		colorService.setEntityClass(Color.class);
    		colorService.setPKClass(Long.class);
    		Color color = colorService.findUniqueBy("colorId", specialField1);
    		String colorNameN18 = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME,color.getColorName(), FacesMessage.SEVERITY_ERROR).getDetail();
    		result.setSpecialField1Description(colorNameN18);
		}
		if (null != specialField2 && !("".equals(specialField2))) {
    		result.setSpecialField2(specialField2);
    		ISizeService<Size,Long> sizeService = (ISizeService<Size,Long>)SpringFacesUtil.getSpringBean(SpringBeanConstants.SIZE_SERVICE_BEAN_NAME);
    		sizeService.setEntityClass(Size.class);
    		sizeService.setPKClass(Long.class);
    		Size size = sizeService.findUniqueBy("sizeId", specialField2);
    		result.setSpecialField2Description(size.getSizeName());
		}
        
        return result;
    }
}
